﻿//===============================================================================
//	LuaRunScriptFunction.cpp
//	C++ -> LUA LuaRunScriptFunction 接口
//===============================================================================

#include "LuaManager.h"
#include "LuaScriptManager.h"
#include "../share/user_define.h"
#include "ManagerHelper.h"

//	lua脚本设置
std::shared_ptr<LuaScriptManager> g_lua_script_manager = std::make_shared<LuaScriptManager>();

//-------------------------------------------------------------------------------
#define RUN_SCRIPT_BEGIN \
	_safe_guard_begin; \
	if ( ! m_state ) { scriptError(ScriptStateIsNullptr); return 0; } \
	char* _filename = g_lua_script_manager->GetFileNameByScriptId(scriptID); \
	if (! verifyScriptFile(_filename) )	{ return 0; } \
	if ( ! verifyFunction( scriptID, functionName ) ) { return 0; } \
	markScriptName(_filename);\
	StartStep(scriptID, functionName); \
	if ( static_Reload_Script ) { bool _ret = LoadScript(_filename); \
		if ( ! _ret ) { EndStep(scriptID, functionName); return 0; } } \
		int32_t _ret = 0; \
		if ( m_stack.StepStart(m_full_function_name, scriptID) ) { 

//-------------------------------------------------------------------------------
#define RUN_SCRIPT_END \
		m_stack.StepEnd();	}\
	EndStep(scriptID, functionName);\
	return _ret;\
	_safe_guard_end; \
	return 0;

//-------------------------------------------------------------------------------
#define RUN_SCRIPT_STR_BEGIN \
	_safe_guard_begin; \
	if ( ! m_state ) { scriptError(ScriptStateIsNullptr); return ""; } \
	char* _filename = g_lua_script_manager->GetFileNameByScriptId(scriptID); \
	if (! verifyScriptFile(_filename) )	{ return ""; } \
	if ( ! verifyFunction( scriptID, functionName ) ) { return ""; } \
	markScriptName(_filename);\
	StartStep(scriptID, functionName); \
	if ( static_Reload_Script ) { bool _ret = LoadScript(_filename); \
		if ( ! _ret ) { EndStep(scriptID, functionName); return ""; } } \
		std::string _str = "";\
		if ( m_stack.StepStart(m_full_function_name, scriptID) ) { 

//-------------------------------------------------------------------------------
#define RUN_SCRIPT_STR_END \
		m_stack.StepEnd();	}\
	EndStep(scriptID, functionName);\
	return _str;\
	_safe_guard_end; \
	return "";

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, 
	const char* functionName )
{
	RUN_SCRIPT_BEGIN;
	
	_ret = runFileFunction(_filename, m_full_function_name, ">");

	RUN_SCRIPT_END;
}

int32_t LuaManager::RunScriptFunctionString( int32_t scriptID, const char* functionName, Json::Value jsonValue )
{
	RUN_SCRIPT_BEGIN;

	_ret=runFileFunction( _filename, m_full_function_name, "s>", jsonValue.toStyledString().c_str() );
		RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, 
	const char* functionName, int32_t argv1 )
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "i>", argv1 );

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID,
	const char* functionName, uint32_t argv1, uint32_t argv2)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "ii>", argv1, argv2);

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, uint32_t argv1, int32_t argv2)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "ii>", argv1, argv2);

	RUN_SCRIPT_END;
}

int32_t LuaManager::RunScriptFunction ( int32_t scriptID, const char* functionName, int32_t argv1, int32_t argv2 )
{
	RUN_SCRIPT_BEGIN;
	_ret = runFileFunction ( _filename, m_full_function_name, "ii>", argv1, argv2 );
	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, int32_t argv1, double argv2)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "id>", argv1, argv2);

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, double argv1, double argv2)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "dd>", argv1, argv2);

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, 
	const char* functionName, int32_t argv1, int32_t argv2, int32_t argv3 )
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iii>", argv1, argv2, argv3 );

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, int32_t argv1, 
	const char * argv2, const char * argv3)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iss>", argv1, argv2, argv3);

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName
	, int32_t argv1, int32_t argv2, int32_t argv3, int32_t argv4)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iiii>", argv1, argv2, argv3, argv4);

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, double argv1, double argv2, double argv3, double argv4)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "dddd>", argv1, argv2, argv3, argv4);

	RUN_SCRIPT_END;
}

int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, int32_t argv1,
	const char * argv2, const char * argv3, const char * argv4)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "isss>", argv1, argv2, argv3, argv4);

	RUN_SCRIPT_END;
}

int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, int32_t argv1,
	const char * argv2, const char * argv3, const char * argv4, const char * argv5)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "issss>", argv1, argv2, argv3, argv4, argv5);

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, int32_t argv1, int32_t argv2, int32_t argv3, const char * argv4)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iiis>", argv1, argv2, argv3, argv4);

	RUN_SCRIPT_END;
}

int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, int32_t argv1, int32_t argv2, const char * argv3, const char * argv4)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iiss>", argv1, argv2, argv3, argv4);

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, uint32_t argv1
	, uint32_t argv2, uint32_t argv3, uint32_t argv4, uint32_t argv5)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iiiii>", argv1, argv2, argv3, argv4, argv5);

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, int32_t argv1, double argv2, double argv3, double argv4, double argv5)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "idddd>", argv1, argv2, argv3, argv4, argv5);

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName
	, int32_t argv1, int32_t argv2, int32_t argv3, int32_t argv4, int32_t argv5, int32_t argv6)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iiiiii>", argv1, argv2, argv3, argv4, argv5, argv6);

	RUN_SCRIPT_END;
}

int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName
	, int32_t argv1, int32_t argv2, int32_t argv3, int32_t argv4, int32_t argv5, int32_t argv6, int32_t argv7)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iiiiiii>", argv1, argv2, argv3, argv4, argv5, argv6, argv7);

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, 
	const char* functionName, int32_t argv1, double argv2, const char* argv3 )
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "ids>", argv1, argv2, argv3 );

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char* functionName, int32_t argv1,
	int32_t argv2, int32_t argv3, int32_t argv4, const char* argv5)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iiiis>", argv1, argv2, argv3, argv4, argv5);

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char* functionName, int32_t argv1,
	int32_t argv2, int32_t argv3, int32_t argv4, int32_t argv5, const char* argv6)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iiiiis>", argv1, argv2, argv3, argv4, argv5, argv6);

	RUN_SCRIPT_END;
}

int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, int32_t argv1, int32_t argv2, 
	int32_t argv3, int32_t argv4, const char *  argv5, const char * argv6, char * argv7)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iiiisss>", argv1, argv2, argv3, argv4, argv5, argv6, argv7);

	RUN_SCRIPT_END;
}

int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, int32_t argv1, int32_t argv2, int32_t argv3, 
	int32_t argv4, int32_t argv5, int32_t argv6, char * argv7)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iiiiiis>", argv1, argv2, argv3, argv4, argv5, argv6, argv7);

	RUN_SCRIPT_END;
}

int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, int32_t argv1, int32_t argv2, int32_t argv3, 
	int32_t argv4, int32_t argv5, int32_t argv6, const char * argv7, const char * argv8, const char * argv9, const char * argv10)
{
	return int32_t();	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iiiiiissss>", argv1, argv2, argv3, argv4, argv5, argv6, argv7, argv8, argv9, argv10);

	RUN_SCRIPT_END;
}

int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, int32_t argv1, int32_t argv2, int32_t argv3, 
	int32_t argv4, int32_t argv5, int32_t argv6, const char * argv7, const char * argv8)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iiiiiiss>", argv1, argv2, argv3, argv4, argv5, argv6, argv7, argv8);

	RUN_SCRIPT_END;
}

int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, int32_t argv1, int32_t argv2, int32_t argv3, int32_t argv4, 
	int32_t argv5, const char * argv6, const char * argv7, const char * argv8)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iiiiisss>", argv1, argv2, argv3, argv4, argv5, argv6, argv7, argv8);

	RUN_SCRIPT_END;
}

int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, int32_t argv1, int32_t argv2, int32_t argv3, 
	int32_t argv4, int32_t argv5, int32_t argv6, const char * argv7, const char * argv8, const char * argv9)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iiiiiisss>", argv1, argv2, argv3, argv4, argv5, argv6, argv7, argv8, argv9);

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, int32_t argv1, int32_t argv2, 
	int32_t argv3, int32_t argv4, double argv5, const char * argv6)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iiiids>", argv1, argv2, argv3, argv4, argv5, argv6);

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
//int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char* functionName, int32_t argv1,
//	int32_t argv2, int32_t argv3, double argv4, const char* argv5)
//{
//	RUN_SCRIPT_BEGIN;
//
//	_ret = runFileFunction(_filename, m_full_function_name, "iiids>", argv1, argv2, argv3, argv4, argv5);
//
//	RUN_SCRIPT_END;
//}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char* functionName, const char* argv1)
{
	RUN_SCRIPT_BEGIN;

	char m_full_func_name[size_256];
	snprintf(m_full_func_name, size_256 - 1, "S%05d_%s", scriptID, functionName);

	_ret = runFileFunction(_filename, m_full_func_name, "s>", argv1);

	RUN_SCRIPT_END;
}

std::string LuaManager::RunScriptFunctionStringRet(int32_t scriptID, const char* functionName, const char* argv1)
{
	RUN_SCRIPT_STR_BEGIN;

	_str = runFileFunctionStringRet(_filename, m_full_function_name, "s>", argv1);

	RUN_SCRIPT_STR_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, int32_t argv1, const char * argv2)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "is>", argv1, argv2);

	RUN_SCRIPT_END;
}

int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, const char * argv1, const char * argv2)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "ss>", argv1, argv2);

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, int32_t argv1, int32_t argv2, const char * argv3)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iis>", argv1, argv2, argv3);

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, int32_t argv1, int32_t argv2, int32_t argv3, int32_t argv4, const char * argv5, uint16_t argv6)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iiiisi>", argv1, argv2, argv3, argv4, argv5, argv6);

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char* functionName, int32_t argv1, int32_t argv2, int32_t argv3, int32_t argv4, int32_t argv5, int32_t argv6
	, int32_t argv7, int32_t argv8, int32_t argv9, int32_t argv10, int32_t argv11)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iiiiiiiiiii>", argv1, argv2, argv3, argv4, argv5, argv6, argv7
		, argv8, argv9, argv10, argv11);

	RUN_SCRIPT_END;
}

int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, int32_t argv1, int32_t argv2, int32_t argv3, int32_t argv4, int32_t argv5, int32_t argv6, const char * argv7, const char * argv8, const char * argv9, const char * argv10, const char * argv11, const char * argv12)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iiiiiissssss>", argv1, argv2, argv3, argv4, argv5, argv6, argv7
		, argv8, argv9, argv10, argv11, argv12);

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char* functionName, int32_t argv1, int32_t argv2,
	const char* argv3, const char* argv4, uint32_t argv5)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iissi>", argv1, argv2, argv3, argv4, argv5);

	RUN_SCRIPT_END;
}

int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char * functionName, int32_t argv1, int32_t argv2, int32_t argv3, const char * argv4, uint16_t argv5)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iiisi>", argv1, argv2, argv3, argv4, argv5);

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
//int32_t LuaManager::RunScriptFunction(int32_t scriptID, const char* functionName, int32_t argv1, int32_t argv2,
//	const char* argv3, int32_t argv4, uint32_t argv5)
//{
//	RUN_SCRIPT_BEGIN;
//
//	_ret = runFileFunction(_filename, m_full_function_name, "iisii>", argv1, argv2, argv3, argv4, argv5);
//
//	RUN_SCRIPT_END;
//}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunctionWithRet(int32_t scriptID, const char * functionName, 
	int32_t argv1, int32_t * argv2, int32_t * argv3, int32_t * argv4, 
	int32_t * argv5, int32_t * argv6, int32_t * argv7, int32_t * argv8)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "i>iiiiiii", 
		argv1, argv2, argv3, argv4, argv5, argv6, argv7, argv8);

	RUN_SCRIPT_END;
}

//-------------------------------------------------------------------------------
int32_t LuaManager::RunScriptFunctionWithRet(int32_t scriptID, const char * functionName,
	uint32_t argv1, int32_t argv2, 
	double * argv3, double * argv4, double * argv5)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "ii>ddd",
		argv1, argv2, argv3, argv4, argv5);

	RUN_SCRIPT_END;

}

int32_t LuaManager::RunScriptFunctionWithRet(int32_t scriptID, const char* functionName, uint32_t argv1, char* args1)
{
	LOG_DEBUG("default", "core lua debug scriptID:%d, functionName:%s", scriptID, functionName);
	RUN_SCRIPT_BEGIN;
	char m_full_func_name[size_256];
	snprintf(m_full_func_name, size_256 - 1, "S%05d_%s", scriptID, functionName);

	
	
	LOG_DEBUG("default", "core lua debug scriptID:%d, m_full_function_name:%s", scriptID, m_full_function_name);

	_ret = runFileFunction(_filename, m_full_func_name, "i>s",
		argv1, args1);

	RUN_SCRIPT_END;
}

int32_t LuaManager::RunScriptFunctionWithRet(int32_t scriptID, const char* functionName, uint32_t argv1, const char* args1, int32_t* argv2)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "is>i",
		argv1, args1, argv2);

	RUN_SCRIPT_END;
}

int32_t LuaManager::RunScriptFunctionWithRet(int32_t scriptID, const char* functionName, int32_t argv1, int32_t argv2, int32_t argv3, int32_t* argv4)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "iii>i",
		argv1, argv2, argv3, argv4);

	RUN_SCRIPT_END;
}

int32_t LuaManager::RunScriptFunctionWithRet(int32_t scriptID, const char* functionName, int32_t argv1, const char* argv2, char* argv3)
{
	RUN_SCRIPT_BEGIN;

	_ret = runFileFunction(_filename, m_full_function_name, "is>s",
		argv1, argv2, argv3);

	RUN_SCRIPT_END;
}


//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
